package com.ftg.learn.mall.mapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ftg.learn.mall.dto.pmsProductAttributeParam;
import com.ftg.learn.mall.entity.PmsProductAttribute;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ftg.learn.mall.entity.PmsProductAttributeCategory;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * <p>
 * 商品属性参数表 Mapper 接口
 * </p>
 *
 * @author mallCommon
 * @since 2022-04-19
 */
public interface PmsProductAttributeMapper extends BaseMapper<PmsProductAttribute> {


    /**
     * 根据分类查询规格列表或参数列表
     *
     * @param type 属性类型
     * @param page 分页
     * @return list
     */
    @Select("select a.product_attribute_id,\n" +
            "       b.product_attribute_category_name,\n" +
            "       a.product_attribute_name,\n" +
            "       a.search_type,\n" +
            "       a.input_type,\n" +
            "       a.input_list,\n" +
            "       a.sort,\n" +
            "       a.filter_type,\n" +
            "       a.search_type,\n" +
            "       a.related_status,\n" +
            "       a.hand_add_status\n" +
            "from pms_product_attribute a\n" +
            "         left join pms_product_attribute_category b\n" +
            "                   on a.product_attribute_category_id = b.product_attribute_category_id\n" +
            "where a.type = #{type}")
    List<pmsProductAttributeParam> findAllAndPage(@Param("type") int type, Page<pmsProductAttributeParam> page);


    /**
     * 删除参数表时修改分类表规格数量
     *
     * @param id id
     * @return true
     */
    @Update("update pms_product_attribute_category\n" +
            "set attribute_count = attribute_count - 1\n" +
            "where product_attribute_category_id = #{id}")
    boolean updateAttributeCount(@Param("id") Long id);

    /**
     * 删除参数表时修改分类表参数数量
     *
     * @param id id
     * @return true
     */
    @Update("update pms_product_attribute_category\n" +
            "set param_count = param_count -1\n" +
            "where product_attribute_category_id = #{id}")
    boolean updateParamCount(@Param("id") Long id);


    /**
     * 根据参数表id 查分类表id
     *
     * @param id
     * @return
     */
    @Select("select product_attribute_category_id from pms_product_attribute where product_attribute_id = #{id}")
    Long selectProductAttributeCategoryId(@Param("id") Long id);


    /**
     * 当添加了一个商品属性的时候，对应产品属性数量+1
     *
     * @param id
     * @return
     */
    @Update("update pms_product_attribute_category set attribute_count = attribute_count + 1 " +
            "where product_attribute_category_id = #{id}")
    int updatettributeCount(@Param("id") Long id);

    @Select("select product_attribute_category_id from pms_product_attribute where product_attribute_id=#{id}")
    Long findid(@Param("id") Long id);

    @Update("update pms_product_attribute_category set param_count = param_count + 1 " +
            "where product_attribute_category_id = #{id}")
    int updateparam(@Param("cid") Long id);

    /**
     * 当删除了一个商品属性的时候，对应产品属性数量-1
     *
     * @param id
     * @return
     */
    @Update("update pms_product_attribute_category set attribute_count = attribute_count - 1 " +
            "where product_attribute_category_id = #{id}")
    int delattribute(@Param("id") Long id);

    @Update("update pms_product_attribute_category set param_count = param_count - 1 " +
            "where product_attribute_category_id = #{id}")
    int delparam(@Param("id") Long id);


    //-----------------------------------------------------NB--------------------------------------------------------------//


    /**
     * 根据id查看属性分类
     * @param id
     * @return
     */
    @Select("select * from pms_product_attribute_category where product_attribute_category_id = #{id}")
    PmsProductAttributeCategory findPmsProductAttributeCategory(@Param("id")Long id);


    /**
     * 根据id查看属性参数
     * @param id
     * @return
     */
    @Select("select * from pms_product_attribute where product_attribute_id = #{id}")
    PmsProductAttribute findPmsProductAttribute(@Param("id")Long id);



    //-------------------------------------------------------------------------------------------------------------------//
}

